package com.yunhe.inspect.service.impl;

import cn.hutool.core.util.StrUtil;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.authority.Organization;
import com.yunhe.common.model.authority.User;
import com.yunhe.common.util.CollectionUtil;
import com.yunhe.inspect.client.AuthorityClient;
import com.yunhe.inspect.service.HelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class HelpServiceImpl implements HelpService {
    @Autowired
    private AuthorityClient authorityClient;

    private Map<String,String> cacheMap = new HashMap<>();

    @Override
    public List<Long> getOrgIds(String orgId, Long stationId, Long userId) {
        //如果有传入orgId 就返回此orgId
        if(StrUtil.isNotBlank(orgId)){
            return Arrays.asList(Long.valueOf(orgId));
        }

        //否则查询此用户所在的部门
        Assert.isTrue(Objects.nonNull(stationId), "请选择项目查询巡检计划!");
        Assert.isTrue(Objects.nonNull(userId), "用户登录信息已过期，请重新登录！");

        /*Long headerUser = myHeaderUtil.getHeaderUser();
        Assert.isTrue(Objects.nonNull(headerUser), "用户登录信息已过期，请重新登录！");*/

        try {
            //查询当前登录人的部门以及负责部门和子部门
            ResponseEntity<ResultObject<List<Organization>>> responseEntity = authorityClient.getSubOrgByLoginUser(stationId, userId,true);
            if(Objects.isNull(responseEntity) || !responseEntity.getStatusCode().is2xxSuccessful()){
                return null;
            }
            List<Organization> organizations = responseEntity.getBody().getResults();
            if(CollectionUtil.isNotEmpty(organizations)){
                return organizations.stream().map(Organization::getId).collect(Collectors.toList());
            }
            return null;
        } catch (Exception e){
            return null;
        }
    }

    @Override
    public String getUserName(String userId) {
        String res = null;
        if(StrUtil.isBlank(userId)) return res;

        String key = "UserName:" +userId;
        //有缓存 直接返回
        if(Objects.nonNull(cacheMap.get(key))){
            return cacheMap.get(key);
        }

        //调接口查询
        ResponseEntity<ResultObject<User>> resEntity = authorityClient.getUserById(Long.valueOf(userId));
        if(Objects.isNull(resEntity) || !resEntity.getStatusCode().is2xxSuccessful() || Objects.isNull(resEntity.getBody())){
            return null;
        }
        User user = resEntity.getBody().getResults();

        //存缓存 并返回
        cacheMap.put(key, user.getTitle());
        return user.getTitle();
    }

    @Override
    public String getUserName(String userId, String dft) {
        String username = this.getUserName(userId);
        if(Objects.isNull(username)){
            return dft;
        }
        return username;
    }

}
